package com.emuporium.gymrat.server.routine;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.RoutineDB;

@SuppressWarnings("serial")
public class RoutineEditEntrySubmitServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id = new Integer(request.getParameter("id"));
		int order = new Integer(request.getParameter("order"));
		int sets = new Integer(request.getParameter("sets"));
		int reps = new Integer(request.getParameter("reps"));
		int routineId = new Integer(request.getParameter("routineid"));
		String notes = request.getParameter("notes");
		String variationId[] = request.getParameterValues("variation");
		ArrayList<Integer> ids = new ArrayList<Integer>();

		if (variationId != null)
			for (String s : variationId) {
				ids.add(new Integer(s));
			}

		RoutineDB.updateEntry(id, order, sets, reps, notes, ids);

		request.setAttribute("redirecturl", "/routine/modify?id="+routineId);
		String url = "/includes/redirect.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}
}
